
import Request from '@/js_sdk/luch-request/index.js'
import {
	BASE_URL
} from '@/config/url.js'
// import qs from 'qs'
const http = new Request({
	baseURL: BASE_URL, //设置请求的base url
	timeout: 300000, //超时时长5分钟,
	header: {
		'Content-Type': 'application/json; charset=utf-8',
		'Access-Token': ''
	}
})

//请求拦截器
http.interceptors.request.use((config) => { // 可使用async await 做异步操作

	const Authorization = uni.getStorageSync('token');
	if (Authorization) {
		config.header['Access-Token'] = Authorization;
		config.header.Authorization = "Bearer " + Authorization;
	}
	if (config.method === 'POST') {
		config.data = JSON.stringify(config.data);
	}
	if (config.method === 'GET') {
		config.data = JSON.stringify(config.data);
	}
	uni.showLoading({
		title: '加载中，请稍后....'
	});
	return config
}, error => {
	return Promise.resolve(error)
})

// 响应拦截器
http.interceptors.response.use((response) => {
	uni.hideLoading();
	if ((response.data.status == 401 && response.data)) {
		uni.clearStorageSync();
		uni.navigateTo({
			url: "/pages/login/login"
		})
		uni.showToast({
			title: response.data.message,
			icon: "none",
			duration: 2000
		});
	}
	return response.data;
}, (error) => {
	/* //未登录时清空缓存跳转
	if (error.statusCode == 401) {
		uni.clearStorageSync();
		uni.switchTab({
			url: "/pages/home/index/index"
		})
	} */
	return Promise.resolve(error)
})
export default http;